package com.tjetc.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjetc.common.JsonResult;
import com.tjetc.dao.BedInfoMapper;
import com.tjetc.dao.CareContentMapper;
import com.tjetc.dao.ElderInfoMapper;
import com.tjetc.entity.Admin;
import com.tjetc.entity.BedInfo;
import com.tjetc.entity.CareContent;
import com.tjetc.entity.ElderInfo;
import com.tjetc.service.BedInfoService;
import com.tjetc.service.CareContentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class CareContentServiceImpl implements CareContentService {
    @Autowired
    private CareContentMapper careContentMapper;
    @Override
    public JsonResult findById(Long id) {
        CareContent careContent=careContentMapper.selectById(id);
        return JsonResult.success(careContent);
    }
    @Override
    public JsonResult findPage(String name,Long type,Long status, Integer pageNo, Integer pageSize) {
        //设置页码和每页数量
        Page<CareContent> page=Page.of(pageNo,pageSize);
        //把adminPage传入mapper查询中，adminPage就是最后的分页结果
        Page<CareContent>careContentPage= careContentMapper.selectPageLikeName(page,name,type,status);
        //convert方法转换Page中 records集合元素类型 Admin---->AdminView
        //new Function<Admin,AdminView>(){};表示创建一个实现Function的匿名类 对象
        return JsonResult.success(careContentPage);
    }
    @Override
    public JsonResult deleteById(Long id) {
        int rows=careContentMapper.deleteById(id);
        if (rows>0){
            return JsonResult.success("删除成功");
        }
        else {
            return JsonResult.fail("删除失败");
        }
    }
    //@Transactional用在service方法上，保证事务
    //注意：默认情况@Transactional只对运行时异常起作用(RuntimeException)
    @Transactional
    @Override
    public JsonResult save(CareContent careContentParam) {
        CareContent careContent=null;
        LocalDateTime now=LocalDateTime.now();
        //通过判断id值有无判断是新增还是更新，id值无是新增，id值有就是更新
        if(careContentParam.getCareItemId()==null||careContentParam.getCareItemId()<=0){
            //新增
            careContent=new CareContent();
            BeanUtils.copyProperties(careContentParam,careContent);
            careContent.setCreateTime(now);
            careContent.setUpdateTime(now);
            careContentMapper.insert(careContent);
            return JsonResult.success("新增成功",null);
        }else{
            //更新
            //根据id查询Admin信息
            careContent=careContentMapper.selectById(careContentParam.getCareItemId());
            if(careContent==null){
                return JsonResult.fail("用户不存在，更新失败");
            }
            //要更新的字段
            careContent.setUpdateTime(now);
            careContent.setDescription(careContentParam.getDescription());
            careContent.setDifficulty(careContentParam.getDifficulty());
            careContent.setDuration(careContentParam.getDuration());
            careContent.setFrequency(careContentParam.getFrequency());
            careContent.setIsDeleted(careContentParam.getIsDeleted());
            careContent.setName(careContentParam.getName());
            careContent.setPrice(careContentParam.getPrice());
            careContent.setRequiredSkills(careContentParam.getRequiredSkills());
            careContent.setStatus(careContentParam.getStatus());
            careContent.setType(careContentParam.getType());
            int rows=careContentMapper.updateById(careContent);
            if (rows>0){
                return JsonResult.success("更新成功",null);
            }else {
                return JsonResult.fail("用户不存在，更新失败");
            }
        }
    }
}
